import React from "react";
import {Tooltip} from "antd";
import {messages} from "share/common";

export function ExpandIcon(props) {
  const { onExpandAllRows, expandedAll, showIcon, style } = props;
  return showIcon ? (
    <Tooltip title={expandedAll ? messages('common-1.key25')/*一键收起*/ : messages('common-1.key26')/*一键展开*/} placement="topLeft">
      <div
        className='table-container-all-icon'
        style={style}
        onClick={onExpandAllRows}
      >
        {expandedAll ? '-' : '+'}
      </div>
    </Tooltip>) : <span/>;
}

export function wrappedExpandAllRow(props, callBack) {
  const {rowKey, dataSource, expandedAll, expandedRowRender, expandedRowKeys } = props;

  let showIcon = dataSource.find(item => expandedRowRender && expandedRowRender(item));

  let findKey = record =>{
    return (typeof rowKey === 'string') ? record[rowKey] : rowKey(record)
  };

  let onExpandAllRows = () => {
    let expandedRowKeys = [];
    if(!expandedAll)
      dataSource.map(record => {
        if(expandedRowRender && expandedRowRender(record))
          expandedRowKeys.push(findKey(record));
      });
    callBack({ expandedRowKeys, expandedAll: !expandedAll });
  };

  let onExpand = (expanded, record) => {
    let key = findKey(record);
    if(expanded) {
      expandedRowKeys.push(key);
    } else {
      expandedRowKeys.delete(key);
    }
    let hasExpandedNumber = dataSource.filter(record => expandedRowRender && expandedRowRender(record)).length;
    callBack({ expandedRowKeys, expandedAll: hasExpandedNumber === expandedRowKeys.length });
  };

  return { showIcon, onExpand, onExpandAllRows }
}
